/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.panel.grid;

import org.rails.extjs.Option;
import org.rails.extjs.data.Store;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.panel.Panel;
import org.rails.extjs.panel.grid.column.Column;

public class GridPanel extends Panel {
	
	public GridPanel(){
		super();
	}

	public GridPanel(Store store,ColumnModel colModel) {
		super();
		setColumnModel(colModel);
		setStore(store);
	}

	public GridPanel(Option configOptions) {
		super(configOptions);
	}

	public GridPanel(String title,Store store,ColumnModel colModel) {
		super(title);
		setColumnModel(colModel);
		setStore(store);
	}

	public GridPanel(String id, String title,Store store,ColumnModel colModel) {
		super(id, title);
		setColumnModel(colModel);
		setStore(store);
	}	

	public void setAutoExpandColumn(String autoExpandColumn){
		addOption("autoExpandColumn",autoExpandColumn);
	}
	
	public String getAutoExpandColumn(){
		return getString("autoExpandColumn","");
	}
	
	public void setAutoExpandMax(Integer autoExpandMax){
		addOption("autoExpandMax", autoExpandMax);
	}
	
	public Integer getAutoExpandMax(){
		return getInteger("autoExpandMax", 1000);
	}
	
	public void setAutoExpandMin(Integer autoExpandMin){
		addOption("autoExpandMin", autoExpandMin);
	}
	
	public Integer getAutoExpandMin(){
		return getInteger("autoExpandMin", 50);
	}
	
	public void setColumnModel(ColumnModel colModel){
		addOption("colModel", colModel);
	}
	
	public ColumnModel getColumnModel(){
		return (ColumnModel)getOption("colModel");
	}
	
	public void setColumnLines(Boolean columnLines){
		addOption("columnLines", columnLines);
	}
	
	public Boolean isColumnLines(){
		return getBoolean("columnLines", false);
	}
	
	public void setDDGroup(String ddGroup){
		addOption("ddGroup", ddGroup);
	}
	
	public String getDDGroup(){
		return getString("ddGroup", "GridDD");
	}
	
	public void setDDText(String ddText){
		addOption("ddText", ddText);
	}
	
	public String getDDText(){
		return getString("ddText", "{0} selected row{1}");
	}
	
	public void setDeferRowRender(Boolean deferRowRender){
		addOption("deferRowRender", deferRowRender);
	}
	
	public Boolean isDeferRowRender(){
		return getBoolean("deferRowRender", true);
	}
	
	public void setDisableSelection(Boolean disableSelection){
		addOption("disableSelection", disableSelection);
	}
	
	public Boolean isDisableSelection(){
		return getBoolean("disableSelection", false);
	}
	
	public void setEnableColumnHide(Boolean enableColumnHide){
		addOption("enableColumnHide", enableColumnHide);
	}
	
	public Boolean isEnableColumnHide(){
		return getBoolean("enableColumnHide", true);
	}
	
	public void setEnableColumnMove(Boolean enableColumnMove){
		addOption("enableColumnMove", enableColumnMove);
	}
	
	public Boolean isEnableColumnMove(){
		return getBoolean("enableColumnMove", true);
	}
	
	public void setEnableColumnResize(Boolean enableColumnResize){
		addOption("enableColumnResize", enableColumnResize);
	}
	
	public Boolean isEnableColumnResize(){
		return getBoolean("enableColumnResize", true);
	}
	
	public void setEnableDragDrop(Boolean enableDragDrop){
		addOption("enableDragDrop", enableDragDrop);
	}
	
	public Boolean isEnableDragDrop(){
		return getBoolean("enableDragDrop", false);
	}
	
	public void setEnableHdMenu(Boolean enableHdMenu){
		addOption("enableHdMenu", enableHdMenu);
	}
	
	public Boolean isEnableHdMenu(){
		return getBoolean("enableHdMenu", true);
	}
	
	public void setHideHeaders(Boolean hideHeaders){
		addOption("hideHeaders", hideHeaders);
	}
	
	public Boolean isHideHeaders(){
		return getBoolean("hideHeaders", false);
	}
	
	public void setMaxHeight(Integer maxHeight){
		addOption("maxHeight", maxHeight);
	}
	
	public Integer getMaxHeight(){
		return getInteger("maxHeight", 0);
	}
	
	public void setMinColumnWidth(Integer minColumnWidth){
		addOption("minColumnWidth", minColumnWidth);
	}
	
	public Integer getMinColumnWidth(){
		return getInteger("minColumnWidth", 25);
	}
	
	public void setSelectModel(AbstractSelectionModel selModel){
		addOption("selModel",selModel);
	}
	
	public AbstractSelectionModel getSelectModel(){
		if(getOption("selModel") == null){
			return new RowSelectionModel();
		}else if(getOption("selModel") instanceof RowSelectionModel){
			return (RowSelectionModel)getOption("selModel");
		}else if(getOption("selModel") instanceof CellSelectionModel){
			return (CellSelectionModel)getOption("selModel");
		}else if(getOption("selModel") instanceof CheckboxSelectionModel){
			return (CheckboxSelectionModel)getOption("selModel");
		}else{
			return (AbstractSelectionModel)getOption("selModel");
		}
	}
	
	public void setStore(Store store){
		addOption("store",store);
	}
	
	public Store getStore(){
		if(getOption("store") instanceof Store)
			return (Store) getOption("store");
		else
			return null;
	}
	
	public void setStore(String storeId){
		addOption("store",new JavaScript("Ext.StoreMgr.lookup('{0}')",storeId));
	}
	
	public void setStripeRows(Boolean stripeRows){
		addOption("stripeRows", stripeRows);
	}
	
	public Boolean isStripeRows(){
		return getBoolean("stripeRows", false);
	}
	
	public void setView(GridView view){
		addOption("view", view);
	}
	
	public GridView getView(){
		return (GridView)getOption("view");
	}
	
	public void setViewConfig(Option viewConfig){
		addOption("viewConfig",viewConfig);
	}
	
	public Option getViewConfig(Option viewConfig){
		return (Option)getOption("viewConfig");
	}
	
	public void setTrackMouseOver(Boolean trackMouseOver){
		addOption("trackMouseOver", trackMouseOver);
	}
	
	public Boolean isTrackMouseOver(){
		return getBoolean("trackMouseOver", true);
	}
	
	public Column getCheckboxColumn(){
		return new Column(){
			public String getScript(){
				return "this.sm";
			}
		};
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.grid.GridPanel(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
	

}
